<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/base/jsp/include.jsp"%>
<script type="text/javascript">
	var grid;
	var addFun = function() {
		var rows = grid.datagrid('getSelections');
		if (rows.length != 1) {
			parent.$.messager.w('请选择一个父节点！');
			return;
		}
		if (rows[0].code.length == 6) {
			parent.$.messager.w('最多添加到第二级');
			return;
		}
		var dialog = parent.sy.modalDialog({
			title : '添加帖子分类信息',
			width : 600,
			height : 400,
			url : sy.contextPath + '/go.do?path=social/cateEdit&parent='+rows[0].code+'&level='+(rows[0].level+1),
			buttons : [ {
				text : '添加',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
				}
			} ]
		});
	};
	var editFun = function(id) {
		var rows = grid.datagrid('getSelections');
		if (rows.length != 1) {
			parent.$.messager.alert('请选择一条记录进行编辑！');
			return;
		}
		if(rows[0].code == '0'){
			parent.$.messager.w('根节点无法编辑！');
			return;
		}
		var dialog = parent.sy.modalDialog({
			title : '编辑分类信息',
			width : 600,
			height : 400,
			url : sy.contextPath + '/go.do?path=social/cateEdit&id=' + rows[0].id,
			buttons : [ {
				text : '编辑',
				handler : function() {
					dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
				}
			} ]
		});
	};
	var delFun = function(id) {
		var rows = grid.datagrid('getSelections');
		if (rows.length == 0) {
			parent.$.messager.w('请选择需要删除的记录！');
			return;
		}
		if(rows[0].code == '0'){
			parent.$.messager.w('根节点无法删除！');
			return;
		}
		parent.$.messager.confirm('询问', '您确定要删除此记录？', function(r) {
			if (r) {
				var ids = [];
				for ( var i = 0, l = rows.length; i < l; i++) {
					var r = rows[i];
					ids.push(r.code);
				}
				var id = ids.join(',');
				
				$.post(sy.contextPath + '/social/cateDel.do', {
					code : id
				}, function() {
					grid.treegrid('reload');
				}, 'json');
			}
		});
	};
	var ad = function(code,title) {
		var dialog = parent.sy.modalDialog({
			title : '推广设置-'+title,
			width : 1000,
			height : 600,
			url : sy.contextPath + '/go.do?path=social/adList&cateCode=' + code
		});
	};
	var redoFun = function() {
		var node = grid.treegrid('getSelected');
		if (node) {
			grid.treegrid('expandAll', node.id);
		} else {
			grid.treegrid('expandAll');
		}
	};
	var undoFun = function() {
		var node = grid.treegrid('getSelected');
		if (node) {
			grid.treegrid('collapseAll', node.id);
		} else {
			grid.treegrid('collapseAll');
		}
	};
	$(function() {
		grid = $('#grid').treegrid({
			url : sy.contextPath + '/social/cateTree.do',
			idField : 'code',
			treeField : 'title',
			parentField : 'parentCode',
			toolbar : '#toolbar',
			pagination : false,
			frozenColumns : [ [ {
				width : $(this).width() * 0.2,
				title : '名称',
				align : 'left',
				halign:'center',
				field : 'title'
			} ] ],
			columns : [ [{
				width : $(this).width() * 0.15,
				title : '编号',
				align : 'center',
				field : 'code'
			},{
				width : $(this).width() * 0.2,
				title : 'logo',
				align : 'center',
				field : 'logo',
				formatter : function(value, row, index) {
					return po.showImg(value,18,18);
				}
			}, {
				width : $(this).width() * 0.1,
				title : '排序号(倒序)',
				field : 'sortOrder',
				align : 'center'
			}, {
				width : $(this).width() * 0.2,
				title : '创建时间',
				field : 'createTime',
				align : 'center'
			},{
				width : $(this).width() * 0.15,
				title : '操作',
				align : 'center',
				field : 'op',
				formatter : function(value, row, index) {
					if(row.code != '0'){
						return "[<a href='javascript:;' onclick=\"ad('"+row.code+"','"+row.title+"')\">推广设置</a>]";
					}else{
						return "";
					}
						
				}
			}
			]]
		});
	});
</script>
</head>
<body class="easyui-layout" data-options="fit:true,border:false">
	<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="addFun();">添加</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-table_edit',plain:true" onclick="editFun();">修改</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-table_delete',plain:true" onclick="delFun();">删除</a></td>
			</tr>
		</table>
	</div>
	<div data-options="region:'center',fit:true,border:false">
		<table id="grid" data-options="fit:true,border:false"></table>
	</div>
</body>
</html>